package com.coffeeloversupportlibrary.coffeelover.mercury.my;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.drinked.CoffeeDrinkedActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.exchangerecord.ExChangeRecordActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.shop.ShopActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.my.want.CoffeeWantActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.BlurTransformation;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.radarmap.RadarMapView;

/**
 * @author 盛守义
 * @createTime 2021/11/2 8:47
 * @projectName TurningCoffee
 * @className CenterActivity.java
 * @description TODO
 */
public class CenterFragment extends Fragment {
    private AppCompatButton btn_mybean, btn_mymedal;
    private TextView tv_name, tv_signature, tv_mybean, tv_mymedal, tv_wantNum, tv_drinkedNum;
    private ImageView iv_backimg, civ_avatar;
    private LinearLayout ll_share, ll_message, ll_set, ll_favorite, ll_want, ll_drinked, ll_exshop;
    private RadarMapView dnamain;

    private Intent intent;
    private Activity activity = super.getActivity();
    private View view;

    private User user;

    private int[] tasteValue;
    private int coffeeBeans;

    private JsonUtils jsonUtils;

    public CenterFragment(){}

    public CenterFragment(Activity activity) {
        this.activity=activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.my_activity_main, container,false);
        initViews();
        setListener();
        getData();
        setUserMessage(user);

        //设置广播监听
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("shopCoffeeBean");
        activity.registerReceiver(mRefreshBroadcastReceiver, intentFilter);

        return view;
    }

//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.my_activity_main);
//
//        //user = getIntent().getParcelableExtra("userInfo");
//
//        initViews();
//        setListener();
//        getData();
//        setUserMessage(user);
//
//        //设置广播监听
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("shopCoffeeBean");
//        activity.registerReceiver(mRefreshBroadcastReceiver, intentFilter);
//    }


    void setListener(){
        MyOnClickListerner listerner = new MyOnClickListerner();
        civ_avatar.setOnClickListener(listerner);
        ll_set.setOnClickListener(listerner);
        btn_mymedal.setOnClickListener(listerner);
        btn_mybean.setOnClickListener(listerner);

        ll_want.setOnClickListener(listerner);
        ll_drinked.setOnClickListener(listerner);

        ll_exshop.setOnClickListener(listerner);

        ll_share.setOnClickListener(listerner);

    }

    class MyOnClickListerner implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.civ_avatarmain://个人信息
                    intent = new Intent(activity, UserMessageActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;

                case R.id.ll_sharemain://分享

                    break;

                case R.id.ll_chatmain: //消息

                    break;

                case R.id.ll_setmain://设置,进入设置界面
                    intent = new Intent(activity, SettingsActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;

                //我想品尝的咖啡
                case R.id.ll_wantmain:
                    intent = new Intent(activity, CoffeeWantActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;
                //我喝过的咖啡
                case R.id.ll_drinkedmain:
                    intent = new Intent(activity, CoffeeDrinkedActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;


                case R.id.btn_mybeanmain://咖啡豆，去兑换
                    intent = new Intent(activity, ShopActivity.class);
                    intent.putExtra("userInfo", user);
                    startActivity(intent);
                    break;
                case R.id.btn_mymedalmain://勋章，查看勋章
                    break;

                case R.id.ll_exshopmain://兑换记录，现在没有等待代替
                    intent = new Intent(activity, ExChangeRecordActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    }

    void getData(){
        SharedPreferences userInfo = getActivity().getSharedPreferences("user", 0);
        Log.e("CenterActivity","shareUser：" + userInfo.getString("userInfo", ""));
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));
        Log.e("CenterActivity","username：" +  user.getData().getUser().getUserNickname());
        coffeeBeans = userInfo.getInt("coffeeBean", 0);
        Log.e("CenterActivity","coffeeBeans：" +  coffeeBeans+"");
    }

    void setUserMessage(User user){

        tv_name.setText(user.getData().getUser().getUserNickname());
        tv_signature.setText("最爱："+user.getData().getUser().getUserFavouriteCoffee());
        tv_mybean.setText(coffeeBeans+" 个");
        tv_wantNum.setText(""+user.getData().getUser().getWantNum());
        tv_drinkedNum.setText(""+user.getData().getUser().getTastedNum());

        RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.avater_error).error(R.drawable.avater_error);
        //实现景深背景以及圆形头像   user.getData().getUser().getUserImg()
        Glide.with(activity).load(R.drawable.avatar).apply(requestOptions).into(civ_avatar);

        RequestOptions requestOptions1 = new RequestOptions().error(R.drawable.avatar);
        //景深
        Glide.with(activity).load(R.drawable.avatar).apply(RequestOptions.bitmapTransform(new BlurTransformation(100, 2))).into(iv_backimg);

        //tasteValue = new int[]{user.getData().getUser().getUserSweet(), user.getData().getUser().getUserBitterness(), user.getData().getUser().getUserAcidity(), user.getData().getUser().getUserAlcohol(), user.getData().getUser().getUserAftertaste()};
        tasteValue = new int[] {30, 40, 50, 60, 70};
        dnamain.setValues(tasteValue);
        dnamain.start();
    }

    void initViews() {
        jsonUtils = new JsonUtils();
        ll_message = view.findViewById(R.id.ll_chatmain);
        ll_share = view.findViewById(R.id.ll_sharemain);
        ll_set = view.findViewById(R.id.ll_setmain);
        btn_mybean = view.findViewById(R.id.btn_mybeanmain);
        btn_mymedal = view.findViewById(R.id.btn_mymedalmain);
        iv_backimg = view.findViewById(R.id.iv_backimgmain);
        civ_avatar = view.findViewById(R.id.civ_avatarmain);
        tv_name = view.findViewById(R.id.tv_namemain);
        tv_signature = view.findViewById(R.id.tv_signaturemain);
        tv_mybean = view.findViewById(R.id.tv_mybeanmain);
        tv_mymedal = view.findViewById(R.id.tv_mymedalmain);
        ll_drinked = view.findViewById(R.id.ll_drinkedmain);
        ll_want = view.findViewById(R.id.ll_wantmain);
        ll_exshop = view.findViewById(R.id.ll_exshopmain);
        tv_drinkedNum = view.findViewById(R.id.tv_drinkednum);
        tv_wantNum = view.findViewById(R.id.tv_wantnum);

        //Glide.with(this).load(R.drawable.avatar).apply(RequestOptions.bitmapTransform(new BlurTransformation(100, 2))).into(iv_backimg);

        dnamain = view.findViewById(R.id.ll_dnamain);

        SharedPreferences userInfo;
        userInfo = getActivity().getSharedPreferences("user", 0);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("userInfo", "{\"success\":true,\"code\":20000,\"message\":\"成功\",\"data\":{\"user\":{\"userId\":\"beae50d2-5a6b-4eb4-a498-18e18e97b7cc\",\"userTel\":\"qwe@qq.omc\",\"userNickname\":\"Test2\",\"userFavouriteCoffee\":null,\"userImg\":\"https://graph.baidu.com/pcpage/similar?originSign=1216bd7a94ed338dd79ef01635164538&srcp=crs_pc_similar&tn=pc&idctag=tc&sids=10004_10512_10522_10919_10913_11006_10924_10905_10018_10901_10941_10906_11013_10970_10968_10974_11031_17851_17071_18009_18100_17201_17202_18301_18310_18330_19300_19199_19162_19220_19210_19212_19215_19217_19218_19255_19230_19268_19281_19550_19660_19670_9999_10000&logid=2753907589&entrance=general&tpl_from=pc&pageFrom=graph_upload_pcshitu&image=https%3A%2F%2Fgraph.baidu.com%2Fthumb%2Fv4%2F120482536%2C1109830618.jpg&carousel=503&index=26&page=5&shituToken=db4663\",\"userSweet\":null,\"userBitterness\":null,\"userAcidity\":null,\"userAlcohol\":null,\"userAftertaste\":null,\"wantNum\":0,\"tastedNum\":0,\"coffeeBean\":0}}}");
        editor.putInt("coffeeBean", 0);
        editor.commit();


    }

    private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("shopCoffeeBean"))
            {
                int changedCoffeeBeans = activity.getSharedPreferences("user", 0).getInt("coffeeBean", 0);
                Log.e("CenterActivity", "onReceive: "+ changedCoffeeBeans );
                tv_mybean.setText(changedCoffeeBeans+" 个");
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        activity.unregisterReceiver(mRefreshBroadcastReceiver);
    }
}
